package com.chen.spider.config;

import com.chen.spider.dto.SpiderEntity;
import com.chen.spider.dto.SpiderMainPageEntity;
import com.chen.spider.service.SpiderService;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.*;

@Component
public class MyPipeline implements Pipeline {
    List<SpiderEntity> lists = new ArrayList<>();
//    @Resource
    SpiderEntity spiderEntity;

//    @Resource
    SpiderMainPageEntity spiderMainPageEntity;

    @Resource
    SpiderService spiderService;
    boolean is = true;

    /**
     * 方法一
     * 将数据存入两个表中时对应的pipeline  (函数主页面的链接标题存一个表，子页面内容存一个表)     使用时请将函数名修改为process
     */
    public void process(ResultItems resultItems, Task task) {
        /*原生Pipeline打印的源码*/
        System.out.println("get page:" + resultItems.getRequest().getUrl());
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            System.out.println(entry.getKey() + ":\t" + entry.getValue());
        }

        spiderEntity = resultItems.get("repo");
        if (spiderEntity.getTitle() != null) {
            spiderService.insert(spiderEntity);
        }
    }


    /**
     * 方法二 （参考了秦辉的思路）
     * 将数据存入一个表中时对应的pipeline        使用时请将函数名修改为process
     */
    public void process1(ResultItems resultItems, Task task) {
        /*原生Pipeline打印的源码*/
        System.out.println("get page:" + resultItems.getRequest().getUrl());
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            System.out.println(entry.getKey() + ":\t" + entry.getValue());
        }
        SpiderEntity spiderEntity = resultItems.get("repo");
        spiderService.insert(spiderEntity);

    }


}
